<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/6/17
 * Time: 10:46
 */
namespace app\admin\controller;
use think\Db;
class Question extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['que_title|que_content'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('question')
                ->where($map)
                ->count();
            $lists =  Db::name('question')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('que_sort desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('question/index');
    }


    public function questionAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'que_title'=>$title,
                'que_content'=>$content,
                'que_sort'=>$sort,
                'que_add_time'=>date('Y-m-d H:i:s'),
                'que_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('question')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>"添加成功"]);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('question/question_add');
    }

    public function questionEdit(){
        extract (input());
        if(request()->isPost ()){
            extract (input());
            $param = [
                'que_title'=>$title,
                'que_content'=>$content,
                'que_sort'=>$sort,
                'que_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('question')->where('que_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>"编辑成功"]);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('question')->where(['que_id'=>$id])->find();
        return $this->fetch ('question/question_edit',['data'=>$data]);
    }


    public function questionDel(){
        extract (input());
        $res = Db::name('question')->delete ($id);
        if($res){
            return json(['code'=>200,'msg'=>"删除成功"]);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

}